<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-material.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"blog.yookingh.cn","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":true,"color":"#00000000","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言公司项目需求：为App提供一键优化功能（一键获取相关所有权限），确保App在优化过后能够常驻手机，为用户提供服务。——商户类App，用户非主动关闭时常驻手机合情合理">
<meta property="og:type" content="article">
<meta property="og:title" content="Android辅助功能开发-一键优化">
<meta property="og:url" content="http://blog.yookingh.cn/dev/200424-accessibilityService.html">
<meta property="og:site_name" content="墨染青葱几许烟">
<meta property="og:description" content="前言公司项目需求：为App提供一键优化功能（一键获取相关所有权限），确保App在优化过后能够常驻手机，为用户提供服务。——商户类App，用户非主动关闭时常驻手机合情合理">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2020/04/30/mkMsl5tZ3iz2X4w.jpg">
<meta property="article:published_time" content="2020-04-24T05:29:35.000Z">
<meta property="article:modified_time" content="2020-08-24T01:51:42.322Z">
<meta property="article:author" content="yooking">
<meta property="article:tag" content="学习笔记">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2020/04/30/mkMsl5tZ3iz2X4w.jpg">

<link rel="canonical" href="http://blog.yookingh.cn/dev/200424-accessibilityService.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Android辅助功能开发-一键优化 | 墨染青葱几许烟</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">墨染青葱几许烟</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">左手代码 右手诗意</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-首页">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-时间轴">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>时间轴</a>

  </li>
        <li class="menu-item menu-item-标签云">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签云</a>

  </li>
        <li class="menu-item menu-item-关于&留言">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于&留言</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/yooking-git" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://blog.yookingh.cn/dev/200424-accessibilityService.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/imgs/a_icon_head.jpg">
      <meta itemprop="name" content="yooking">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="墨染青葱几许烟">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Android辅助功能开发-一键优化
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-04-24 13:29:35" itemprop="dateCreated datePublished" datetime="2020-04-24T13:29:35+08:00">2020-04-24</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/dev/" itemprop="url" rel="index"><span itemprop="name">想丶给代码码上诗意</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/dev/200424-accessibilityService.html#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/dev/200424-accessibilityService.html" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>公司项目需求：为App提供一键优化功能（一键获取相关所有权限），确保App在优化过后能够常驻手机，为用户提供服务。——商户类App，用户非主动关闭时常驻手机合情合理<a id="more"></a></p>
<h1 id="阅读源码"><a href="#阅读源码" class="headerlink" title="阅读源码"></a>阅读源码</h1><ol>
<li><p><strong>getWindows()和getRootInActiveWindow()</strong></p>
<p><code>List&lt;AccessibilityNodeInfo&gt; getWindows()</code> 即：获取当前页面所有的节点——包括顶部状态栏、底部系统按钮等各自的root节点。</p>
<p><code>AccessibilityNodeInfo getRootInActiveWindow()</code>即：获取当前活动页面（Activity）中的root节点。</p>
<p><code>getRootInActiveWindow()</code>方法在页面未加载完成时可能为null，而<code>getWindows()</code>一直有值。</p>
</li>
<li><p><strong>AccessibilityNodeInfo</strong></p>
<p><code>AccessibllityNodeInfo</code>类是含有链表结构的类（应该是这样称呼吧）。</p>
<p>譬如：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AccessibilityNodeInfo</span> <span class="keyword">implements</span> <span class="title">Parcelable</span></span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="function"><span class="keyword">public</span> AccessibilityNodeInfo <span class="title">getParent</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"Stub!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> AccessibilityNodeInfo <span class="title">getChild</span><span class="params">(<span class="keyword">int</span> index)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"Stub!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>AccessibllityNodeInfo</code>页包含了对View的操作（仅包含有用到的部分）：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AccessibilityNodeInfo</span> <span class="keyword">implements</span> <span class="title">Parcelable</span></span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="comment">//点击事件</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> ACTION_CLICK = <span class="number">16</span>;</span><br><span class="line">    <span class="comment">//朝后滚动 performAction返回false即滑动到顶部</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> ACTION_SCROLL_BACKWARD = <span class="number">8192</span>;</span><br><span class="line">    <span class="comment">//朝前滚动 performAction返回false即滑动到底部</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> ACTION_SCROLL_FORWARD = <span class="number">4096</span>;</span><br><span class="line">    ...</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">performAction</span><span class="params">(<span class="keyword">int</span> action)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"Stub!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">performAction</span><span class="params">(<span class="keyword">int</span> action, Bundle arguments)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"Stub!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>全局事件</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">AccessibilityService</span> <span class="keyword">extends</span> <span class="title">Service</span> </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="comment">//返回键</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> GLOBAL_ACTION_BACK = <span class="number">1</span>;</span><br><span class="line">    <span class="comment">//Home键</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> GLOBAL_ACTION_HOME = <span class="number">2</span>;</span><br><span class="line">    <span class="comment">//菜单键</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> GLOBAL_ACTION_RECENTS = <span class="number">3</span>;</span><br><span class="line">    ...</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">boolean</span> <span class="title">performGlobalAction</span><span class="params">(<span class="keyword">int</span> action)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"Stub!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ol>
<h1 id="封装工具"><a href="#封装工具" class="headerlink" title="封装工具"></a>封装工具</h1><p>了解完AccessibilityService后，可以对Accessibility进行简单的封装，方便使用。</p>
<ol>
<li><p><strong>获取root节点</strong></p>
<p>由上可知，在寻找目标的root节点这方面，<code>getRootInActiveWindow()</code>会根据有优势，所以</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 获取root节点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> AccessibilityNodeInfo <span class="title">findRoot</span><span class="params">()</span> </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> getRootInActiveWindow();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>获取所有当前Activity可见节点</strong></p>
<p>遍历多叉树</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 遍历多叉树 全遍历</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> List&lt;AccessibilityNodeInfo&gt; <span class="title">iteratorTree</span><span class="params">(AccessibilityNodeInfo parent)</span> </span>&#123;</span><br><span class="line">    List&lt;AccessibilityNodeInfo&gt; childList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    <span class="keyword">if</span> (parent == <span class="keyword">null</span>) <span class="keyword">return</span> childList;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; parent.getChildCount(); i++) &#123;</span><br><span class="line">        AccessibilityNodeInfo child = parent.getChild(i);</span><br><span class="line">        childList.add(child);</span><br><span class="line">        <span class="keyword">if</span> (child.getChildCount() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">//利用递归方法</span></span><br><span class="line">            childList.addAll(iteratorTree(child));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> childList;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>获取全部可见节点</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 遍历当前页面所有节点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> List&lt;AccessibilityNodeInfo&gt; <span class="title">findAllView</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    AccessibilityNodeInfo parent = findRoot();</span><br><span class="line">    List&lt;AccessibilityNodeInfo&gt; list = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    list.add(parent);</span><br><span class="line">    list.addAll(iteratorTree(parent));</span><br><span class="line">    <span class="keyword">return</span> list;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>查找第一个匹配节点</strong></p>
<p>马后炮：在Activity中，可能存在<code>ListView</code>、<code>RecyclerView</code>等<strong>滑动控件</strong>使得目标节点出现<strong>超出屏幕</strong>情况，所以我们应当先在<strong>当前可见区域</strong>内查找是否有目标节点，如果没有，则<strong>向下滚动</strong>，再次查找目标节点——直至滑动到屏幕底部。为防止当前界面<strong>并非从最顶部开始</strong>向下滑动，故应该<strong>再向上滑动</strong>到顶部一次。</p>
<p>查找滑动组件代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> List&lt;String&gt; SCROLL_CLASS_NAME_ARRAY = Arrays.asList(</span><br><span class="line">            <span class="string">"android.widget.ListView"</span>,</span><br><span class="line">            <span class="string">"android.support.v7.widget.RecyclerView"</span>,</span><br><span class="line">            <span class="string">"androidx.recyclerview.widget.RecyclerView"</span></span><br><span class="line">);</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 查找可滑动的view</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> List&lt;AccessibilityNodeInfo&gt; <span class="title">findCanScrollView</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    List&lt;AccessibilityNodeInfo&gt; viewList = findAllView();</span><br><span class="line">    List&lt;AccessibilityNodeInfo&gt; scrollViewList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    <span class="keyword">for</span> (AccessibilityNodeInfo info : viewList) &#123;</span><br><span class="line">        <span class="keyword">if</span> (info != <span class="keyword">null</span>)</span><br><span class="line">            <span class="comment">//info.isScrollable判断是否为可滚动控件</span></span><br><span class="line">            <span class="keyword">if</span> (info.isScrollable()) &#123;</span><br><span class="line">                <span class="comment">//其中滚动控件还包含了spinner、viewpage等,而这些控件目前业务中并没有需要（且会影响业务内容），因此做了个限定</span></span><br><span class="line">                <span class="keyword">for</span> (String scrollClassName : SCROLL_CLASS_NAME_ARRAY) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (scrollClassName.equals(info.getClassName().toString())) &#123;</span><br><span class="line">                        scrollViewList.add(info);</span><br><span class="line">                        setLog(<span class="string">"------------------------------------------------------------"</span>);</span><br><span class="line">                        setLog(<span class="string">"查询到可滑动组件"</span> + info.getViewIdResourceName());</span><br><span class="line">                        setLog(<span class="string">"查询到可滑动组件"</span> + info.getClassName());</span><br><span class="line">                        setLog(<span class="string">"------------------------------------------------------------"</span>);</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> scrollViewList;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>遍历多叉树，根据条件查寻当前可见部分的节点</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 遍历多叉树 查询方法</span></span><br><span class="line"><span class="comment"> * 找到控件会抛出异常，异常中包含目标数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">findIteratorTree</span><span class="params">(AccessibilityNodeInfo parent, String text)</span> <span class="keyword">throws</span> StopIteratorException </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (parent != <span class="keyword">null</span>)</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; parent.getChildCount(); i++) &#123;</span><br><span class="line">            AccessibilityNodeInfo child = parent.getChild(i);</span><br><span class="line">            <span class="keyword">if</span> (child != <span class="keyword">null</span>)</span><br><span class="line">                <span class="keyword">if</span> (multiCriteriaMatching(child, text)) &#123;</span><br><span class="line">                    <span class="keyword">throw</span> <span class="keyword">new</span> StopIteratorException(child);</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (child.getChildCount() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                    findIteratorTree(child, text);</span><br><span class="line">                &#125;</span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 自定义报错 用于中断递归</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">StopIteratorException</span> <span class="keyword">extends</span> <span class="title">RuntimeException</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> AccessibilityNodeInfo info;</span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="title">StopIteratorException</span><span class="params">(AccessibilityNodeInfo info)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.info = info;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">private</span> AccessibilityNodeInfo <span class="title">getInfo</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> info;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 多条件匹配 multiCriteriaMatching</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> info 目前包含getViewIdResourceName、getClassName、getText</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> text 匹配词</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 返回匹配结果</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">multiCriteriaMatching</span><span class="params">(AccessibilityNodeInfo info, String text)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> text.equals(info.getViewIdResourceName())</span><br><span class="line">            || text.equals(info.getClassName() == <span class="keyword">null</span> ? <span class="keyword">null</span> : info.getClassName().toString())</span><br><span class="line">            || text.equals(info.getText() == <span class="keyword">null</span> ? <span class="keyword">null</span> : info.getText().toString());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>查找第一个匹配节点代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> text 要查找目标组件的标识 （className/resourceId/text）</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 返回查找到的目标组件 null则表示查找失败</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> AccessibilityNodeInfo <span class="title">findFirst</span><span class="params">(String text)</span> </span>&#123;</span><br><span class="line">    AccessibilityNodeInfo info = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        findIteratorTree(findRoot(), text);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> searchNum = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">boolean</span> scroll = <span class="keyword">true</span>, isForward = <span class="keyword">true</span>;</span><br><span class="line">        List&lt;AccessibilityNodeInfo&gt; scrollViewList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        <span class="keyword">while</span> (scroll) &#123;<span class="comment">//利用关键字scroll和Exception跳出循环</span></span><br><span class="line">            <span class="comment">//找不到控件，向下滑动继续找</span></span><br><span class="line">            <span class="keyword">if</span> (scrollViewList.size() == <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (searchNum &lt; MAX_SEARCH_NUM) &#123;</span><br><span class="line">                    ++searchNum;</span><br><span class="line">                    scrollViewList.addAll(findCanScrollView());</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    <span class="comment">//找不到可滑动组件</span></span><br><span class="line">                    setLog(<span class="string">"找不到可滑动组件，结束循环体，查找次数:"</span> + MAX_SEARCH_NUM);</span><br><span class="line">                    scroll = <span class="keyword">false</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (scrollViewList.size() != <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="comment">//如果查找到滑动组件，则滑动滑动组件</span></span><br><span class="line">                <span class="comment">//下、上各滑动一次，确保找到控件</span></span><br><span class="line">                <span class="keyword">for</span> (AccessibilityNodeInfo scrollViewInfo : scrollViewList) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (isForward) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (!scrollViewInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD)) &#123;</span><br><span class="line">                            isForward = <span class="keyword">false</span>;<span class="comment">//滑动到底部，修改滑动状态，再滑一次</span></span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                        <span class="keyword">if</span> (!scrollViewInfo.performAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD)) &#123;</span><br><span class="line">                            scroll = <span class="keyword">false</span>;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//滑动后 睡眠 之后查找组件</span></span><br><span class="line">            SystemClock.sleep(TIME_SLEEP);</span><br><span class="line">            findIteratorTree(findRoot(), text);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (StopIteratorException e) &#123;</span><br><span class="line">        info = e.getInfo();</span><br><span class="line">        <span class="keyword">if</span> (info != <span class="keyword">null</span>) &#123;</span><br><span class="line">            setLog(<span class="string">"------------------------------------------------------------"</span>);</span><br><span class="line">            setLog(<span class="string">"查询到目标组件id："</span> + info.getViewIdResourceName());</span><br><span class="line">            setLog(<span class="string">"查询到目标组件text："</span> + info.getText());</span><br><span class="line">            setLog(<span class="string">"查询到目标组件clsName："</span> + info.getClassName());</span><br><span class="line">            setLog(<span class="string">"------------------------------------------------------------"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> info;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>补充说明：（可不看）</strong>在查看AccessibilityNodeInfo类中有如下方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AccessibilityNodeInfo</span> <span class="keyword">implements</span> <span class="title">Parcelable</span></span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;AccessibilityNodeInfo&gt; <span class="title">findAccessibilityNodeInfosByText</span><span class="params">(String text)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"Stub!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;AccessibilityNodeInfo&gt; <span class="title">findAccessibilityNodeInfosByViewId</span><span class="params">(String viewId)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"Stub!"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>由此联想到<code>findIteratorTree(AccessibilityNodeInfo parent, String text)</code>或许可以改成：<strong>（以下代码未尝试，只是临时想到的改进方案）</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> AccessibilityNodeInfo <span class="title">findIteratorTree</span><span class="params">(AccessibilityNodeInfo parent, String text)</span></span>&#123;</span><br><span class="line">    List&lt;AccessibilityNodeInfo&gt; infoList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">    infoList.addAll(parent.findAccessibilityNodeInfosByText(text));</span><br><span class="line">    <span class="keyword">if</span>(info.size() == <span class="number">0</span>)&#123;</span><br><span class="line">        infoList.addAll(parent.findAccessibilityNodeInfosByViewId(text));</span><br><span class="line">    &#125;</span><br><span class="line">    AccessibilityNodeInfo info = <span class="keyword">null</span>;</span><br><span class="line">    <span class="keyword">if</span>(infoList.size() &gt; <span class="number">0</span>)&#123;</span><br><span class="line">    	info = infoList.get(<span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> info;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>点击事件</strong></p>
<p>找到目标节点后，自然是要对目标节点进行操作——这里仅封装点击事件</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 点击事件</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> info 要点击的目标View</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 点击结果（成功/失败）</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">clickView</span><span class="params">(AccessibilityNodeInfo info)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (info != <span class="keyword">null</span>)</span><br><span class="line">        <span class="keyword">if</span> (info.isClickable()) &#123;</span><br><span class="line">            info.performAction(AccessibilityNodeInfo.ACTION_CLICK);</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            AccessibilityNodeInfo parent = info.getParent();</span><br><span class="line">            <span class="keyword">if</span> (parent != <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">boolean</span> b = clickView(parent);</span><br><span class="line">                parent.recycle();</span><br><span class="line">                <span class="keyword">return</span> b;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>将查询节点与点击节点组合起来就是：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 点击事件（点击本身）</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> text 要点击的目标节点标识</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 点击结果（true/false）</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">clickFirstView</span><span class="params">(String text)</span> </span>&#123;</span><br><span class="line">    AccessibilityNodeInfo info = findFirst(text);</span><br><span class="line">    <span class="keyword">if</span> (info == <span class="keyword">null</span>) &#123;</span><br><span class="line">        setLog(text + <span class="string">"找不到匹配的控件"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> clickView(info);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>那如果是CheckBox之类的点击事件（如下图）应该怎么处理？<br><img src="https://i.loli.net/2020/04/30/mkMsl5tZ3iz2X4w.jpg" alt="1.jpg"></p>
<p>首先转化为查询该CheckBox的<strong>同级节点</strong>，再通过该同级节点反查<strong>父节点</strong>，之后再查询<strong>父节点中的子节点</strong>中包含<code>isCheckable</code>的节点：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> AccessibilityNodeInfo <span class="title">findBrotherCheckBox</span><span class="params">(AccessibilityNodeInfo child)</span> </span>&#123;</span><br><span class="line">    AccessibilityNodeInfo checkBoxInfo = <span class="keyword">null</span>;</span><br><span class="line">    AccessibilityNodeInfo parent = child.getParent();</span><br><span class="line">    <span class="keyword">if</span> (parent != <span class="keyword">null</span>) &#123;</span><br><span class="line">        List&lt;AccessibilityNodeInfo&gt; infoList = iteratorTree(parent);</span><br><span class="line">        setLog(<span class="string">"------------------------------------------------------------"</span>);</span><br><span class="line">        <span class="keyword">for</span> (AccessibilityNodeInfo info : infoList) &#123;</span><br><span class="line">            <span class="keyword">if</span> (info.isCheckable()) &#123;</span><br><span class="line">                checkBoxInfo = info;</span><br><span class="line">                setLog(<span class="string">"------------------------------------------------------------"</span>);</span><br><span class="line">                setLog(<span class="string">"查询到目标组件id："</span> + info.getViewIdResourceName());</span><br><span class="line">                setLog(<span class="string">"查询到目标组件text："</span> + info.getText());</span><br><span class="line">                setLog(<span class="string">"查询到目标组件clsName："</span> + info.getClassName());</span><br><span class="line">                setLog(<span class="string">"------------------------------------------------------------"</span>);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        setLog(<span class="string">"------------------------------------------------------------"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> checkBoxInfo;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>CheckBox节点中有<code>isChecked</code>字段用于判断是( true )否( false )选中，因此有：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * checkBox的点击事件（带选中状态）</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> brotherText 要点击目标的兄弟节点标识</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> setChecked  将checkBox的状态修改为true/false</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 返回点击事件触发结果（true/false）</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">clickCheckBox</span><span class="params">(String brotherText, <span class="keyword">boolean</span> setChecked)</span> </span>&#123;</span><br><span class="line">    AccessibilityNodeInfo brotherInfo = findFirst(brotherText);</span><br><span class="line">    <span class="keyword">if</span> (brotherInfo == <span class="keyword">null</span>) &#123;</span><br><span class="line">        setLog(brotherText + <span class="string">"找不到匹配的控件"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    AccessibilityNodeInfo checkBoxInfo = findBrotherCheckBox(brotherInfo);</span><br><span class="line">    <span class="keyword">if</span> (checkBoxInfo == <span class="keyword">null</span>) &#123;</span><br><span class="line">        setLog(brotherText + <span class="string">"找不到匹配的checkBox控件"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (checkBoxInfo.isChecked() != setChecked) &#123;</span><br><span class="line">        <span class="keyword">return</span> clickView(checkBoxInfo);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        setLog(<span class="string">"checkBox控件为目标状态，不点击"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>补充</strong></p>
<p>静态变量、日志与返回</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//静态变量</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> TIME_SLEEP = <span class="number">300</span>;<span class="comment">//单位毫秒</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> MAX_SEARCH_NUM = <span class="number">20</span>;<span class="comment">//查找循环体次数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//日志</span></span><br><span class="line"><span class="function"><span class="keyword">protected</span> <span class="keyword">abstract</span> <span class="keyword">boolean</span> <span class="title">isDebugger</span><span class="params">()</span></span>;</span><br><span class="line"><span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">setLog</span><span class="params">(String msg)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (isDebugger()) &#123;</span><br><span class="line">        String tag = <span class="string">"AManagerLog"</span>;</span><br><span class="line">        Log.i(tag, msg);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//系统返回键</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">goBack</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    performGlobalAction(GLOBAL_ACTION_BACK);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>以上就是整个工具的封装了</p>
</li>
</ol>
<h1 id="如何使用"><a href="#如何使用" class="headerlink" title="如何使用"></a>如何使用</h1><ol>
<li><p><strong>辅助功能触发点的设置</strong></p>
<p>辅助功能顾名思义，就是辅助用户使用当前App，甚至可以延伸为使用当前手机。比如：</p>
<ul>
<li>监测App接收到的通知消息，根据消息对手机进行辅助操作</li>
<li>监测文件下载状态，根据下载状态进行自动安装、打开等操作</li>
<li>当用户点击了某个位置时，根据点击的位置做出反馈</li>
<li>为用户自动开启相关权限等</li>
<li>…</li>
</ul>
<p>这时候：监测到App接收通知消息和监测到文件下载状态即为触发点；用户点击事件即为出发点；而开启权限，则可以是在辅助功能权限开启的时候。</p>
<p><strong>触发点为辅助功能权限开启</strong>：辅助功能权限开启的时候会调用初<strong>始化Service</strong>方法：<code>onServiceConnected()</code>，这里可以作为一个监测点，利用<code>sendBroadcast(Intent intent)</code>将<strong>服务启动</strong>的消息广播出去。而<strong>广播接收器</strong>就是真实的<strong>触发点</strong>。</p>
<p><strong>触发点为某种状态</strong>：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span>(该状态可回调)&#123;</span><br><span class="line">    <span class="keyword">if</span>(辅助功能权限开启)&#123;</span><br><span class="line">        直接执行辅助功能</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        弹出无障碍设置页面，引导用户开启辅助功能权限-触发点转为辅助功能权限开启时</span><br><span class="line">    &#125;</span><br><span class="line">&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    监测状态改变引发的界面变化</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>触发点为用户点击事件</strong>：如果点击的是自己的App，在点击事件触发辅助功能即可。如果点击的不为自己的App，可以在<code>onAccessibilityEvent(AccessibilityEvent event)</code>触发。</p>
</li>
<li><p><strong>辅助功能执行</strong></p>
<p>辅助功能是一个富有想象力的功能，TA的执行能力取决于脑洞。比如模拟用户点击，让用户的操作更快捷（比如自动抢红包、一键连招等）或者取代繁琐操作（自动下载并安装等）甚至也可以是智能分词翻译（锤子BigBang）等等…</p>
<p>这里以“取代繁琐操作”为例——征得用户同意后，替用户勾选一些无法直接申请的权限：</p>
<p>任务：<code>AssignmentEntity</code>：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AssignmentEntity</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Queue&lt;StepEntity&gt; queue = <span class="keyword">new</span> LinkedList();	<span class="comment">//步骤队列</span></span><br><span class="line">    <span class="keyword">private</span> String name;								<span class="comment">//任务名称</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">AssignmentEntity</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> AssignmentEntity <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Queue&lt;StepEntity&gt; <span class="title">getQueue</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.queue;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 添加步骤</span></span><br><span class="line"><span class="comment">     **/</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> AssignmentEntity <span class="title">addStep</span><span class="params">(StepEntity entity)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.queue.add(entity);</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>具体步骤：<code>StepEntity</code>：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StepEntity</span> </span>&#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> TYPE_INTENT = <span class="number">0</span>;    <span class="comment">//页面跳转</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> TYPE_CLICK = <span class="number">1</span>;     <span class="comment">//点击事件</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> TYPE_CHECKED = <span class="number">2</span>;   <span class="comment">//选中事件</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> TYPE_BACK = <span class="number">3</span>;      <span class="comment">//返回事件</span></span><br><span class="line">    <span class="keyword">private</span> String name;      <span class="comment">//目标控件名称</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">int</span> type;         <span class="comment">//事件类型</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">boolean</span> checked;  <span class="comment">//选中事件-true:选中-false:反选</span></span><br><span class="line">    <span class="keyword">private</span> Intent intent;    <span class="comment">//跳转事件-Intent</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">StepEntity</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getType</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.type;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setType</span><span class="params">(<span class="keyword">int</span> type)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.type = type;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Intent <span class="title">getIntent</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.intent;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setIntent</span><span class="params">(Intent intent)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.intent = intent;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isChecked</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.checked;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setChecked</span><span class="params">(<span class="keyword">boolean</span> checked)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.checked = checked;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>步骤辅助类<code>StepHelper</code>：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StepHelper</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">StepHelper</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 跳转事件</span></span><br><span class="line"><span class="comment">     **/</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> StepEntity <span class="title">intentStep</span><span class="params">(Intent intent)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> getStep(<span class="string">"页面跳转"</span>, <span class="number">0</span>, intent, (Boolean)<span class="keyword">null</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 点击事件</span></span><br><span class="line"><span class="comment">     **/</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> StepEntity <span class="title">clickStep</span><span class="params">(String text)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> getStep(text, <span class="number">1</span>, (Intent)<span class="keyword">null</span>, (Boolean)<span class="keyword">null</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 选中事件</span></span><br><span class="line"><span class="comment">     **/</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> StepEntity <span class="title">checkStep</span><span class="params">(String text, <span class="keyword">boolean</span> setChecked)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> getStep(text, <span class="number">2</span>, (Intent)<span class="keyword">null</span>, setChecked);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 返回事件</span></span><br><span class="line"><span class="comment">     **/</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> StepEntity <span class="title">backStep</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> getStep(<span class="string">"返回上一页"</span>, <span class="number">3</span>, (Intent)<span class="keyword">null</span>, (Boolean)<span class="keyword">null</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> StepEntity <span class="title">getStep</span><span class="params">(String name, <span class="keyword">int</span> type, @Nullable Intent intent, @Nullable Boolean setChecked)</span> </span>&#123;</span><br><span class="line">        StepEntity step = <span class="keyword">new</span> StepEntity();</span><br><span class="line">        step.setName(name);</span><br><span class="line">        step.setType(type);</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">0</span> == type) &#123;</span><br><span class="line">            <span class="keyword">if</span> (intent == <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"代码错误，Type为TYPE_INTENT时Intent参数不能为空"</span>);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            step.setIntent(intent);</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (<span class="number">2</span> == type) &#123;</span><br><span class="line">            <span class="keyword">if</span> (setChecked == <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"代码错误，Type为TYPE_CHECKED时setChecked参数不能为空"</span>);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">boolean</span> b = setChecked;</span><br><span class="line">            step.setChecked(b);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> step;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>添加任务示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AssignmentFactory</span> </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Queue&lt;AssignmentEntity&gt; <span class="title">create</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        Queue&lt;AssignmentEntity&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();</span><br><span class="line">        <span class="keyword">if</span> (RomUtils.isHuawei()) &#123;</span><br><span class="line">            queue.addAll(HuaweiFactory.create());</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (RomUtils.isMiui()) &#123;</span><br><span class="line">            queue.addAll(XiaomiFactory.create());</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (RomUtils.isOppo()) &#123;</span><br><span class="line"></span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (RomUtils.isMeizu()) &#123;</span><br><span class="line">            queue.addAll(MeizuFactory.create());</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> queue;</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">        </span><br><span class="line">    <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">HuaweiFactory</span> </span>&#123;</span><br><span class="line">        </span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">getVersion</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">int</span> version = -<span class="number">1</span>;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                String systemProperty = RomUtils.getSystemProperty(<span class="string">"ro.build.version.emui"</span>);</span><br><span class="line">                <span class="keyword">if</span> (systemProperty != <span class="keyword">null</span>) &#123;</span><br><span class="line">                    String trim = systemProperty.replace(<span class="string">"EmotionUI"</span>, <span class="string">""</span>).replace(<span class="string">"_"</span>, <span class="string">""</span>).trim();</span><br><span class="line">                    <span class="keyword">if</span> (trim.contains(<span class="string">"."</span>)) &#123;</span><br><span class="line">                        trim = trim.substring(<span class="number">0</span>, trim.indexOf(<span class="string">"."</span>));</span><br><span class="line">                    &#125;</span><br><span class="line">                    version = Integer.valueOf(trim);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception ignored) &#123;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> version;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">static</span> Queue&lt;AssignmentEntity&gt; <span class="title">create</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            L.i(<span class="string">"手机版本号："</span> + getVersion()); <span class="comment">//目前适配10</span></span><br><span class="line">            Queue&lt;AssignmentEntity&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();</span><br><span class="line">            queue.add(ignoreBatteryOptimization());</span><br><span class="line">            queue.addAll(newMessageNotification());</span><br><span class="line">            queue.add(selfStarting());</span><br><span class="line">            <span class="keyword">return</span> queue;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//忽略电池优化</span></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> AssignmentEntity <span class="title">ignoreBatteryOptimization</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            AssignmentEntity assignment = <span class="keyword">new</span> AssignmentEntity();</span><br><span class="line">            assignment.setName(<span class="string">"忽略电池优化"</span>);</span><br><span class="line"></span><br><span class="line">            assignment.addStep(StepHelper.intentStep(IntentUtils.hightPowerManger()))</span><br><span class="line">                    .addStep(StepHelper.clickStep(<span class="string">"不允许"</span>))</span><br><span class="line">                    .addStep(StepHelper.clickStep(<span class="string">"所有应用"</span>))</span><br><span class="line">                    .addStep(StepHelper.clickStep(BaseApplication.getInstance().getAppName()))</span><br><span class="line">                    .addStep(StepHelper.clickStep(<span class="string">"不允许"</span>))</span><br><span class="line">                    .addStep(StepHelper.clickStep(<span class="string">"确定"</span>))</span><br><span class="line">                    .addStep(StepHelper.backStep());</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> assignment;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//新消息通知</span></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> Queue&lt;AssignmentEntity&gt; <span class="title">newMessageNotification</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            AssignmentEntity ae1 = <span class="keyword">new</span> AssignmentEntity();</span><br><span class="line">            ae1.setName(<span class="string">"新消息通知"</span>);</span><br><span class="line">            AssignmentEntity ae2 = <span class="keyword">new</span> AssignmentEntity();</span><br><span class="line">            ae2.setName(<span class="string">"通知锁屏显示"</span>);</span><br><span class="line"></span><br><span class="line">            ae1.addStep(StepHelper.intentStep(IntentUtils.huaweiNotification()))</span><br><span class="line">                    .addStep(StepHelper.clickStep(BaseApplication.getInstance().getAppName()))</span><br><span class="line">                    .addStep(StepHelper.checkStep(<span class="string">"允许通知"</span>, <span class="keyword">true</span>))</span><br><span class="line">                    .addStep(StepHelper.backStep());</span><br><span class="line">            ae2.addStep(StepHelper.clickStep(<span class="string">"锁屏通知"</span>))</span><br><span class="line">                    .addStep(StepHelper.clickStep(<span class="string">"显示所有通知"</span>))</span><br><span class="line">                    .addStep(StepHelper.clickStep(<span class="string">"更多通知设置"</span>))</span><br><span class="line">                    .addStep(StepHelper.checkStep(<span class="string">"通知亮屏提示"</span>, <span class="keyword">true</span>))</span><br><span class="line">                    .addStep(StepHelper.backStep())</span><br><span class="line">                    .addStep(StepHelper.backStep());</span><br><span class="line"></span><br><span class="line">            Queue&lt;AssignmentEntity&gt; queue = <span class="keyword">new</span> LinkedList&lt;&gt;();</span><br><span class="line">            queue.add(ae1);</span><br><span class="line">            queue.add(ae2);</span><br><span class="line">            <span class="keyword">return</span> queue;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//自启动</span></span><br><span class="line">        <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> AssignmentEntity <span class="title">selfStarting</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            AssignmentEntity assignment = <span class="keyword">new</span> AssignmentEntity();</span><br><span class="line">            assignment.setName(<span class="string">"自启动"</span>);</span><br><span class="line"></span><br><span class="line">            assignment.addStep(StepHelper.intentStep(IntentUtils.huaweiStartupNormalApp()))</span><br><span class="line">                    .addStep(StepHelper.checkStep(BaseApplication.getInstance().getAppName(), <span class="keyword">true</span>))</span><br><span class="line">                    .addStep(StepHelper.checkStep(BaseApplication.getInstance().getAppName(), <span class="keyword">false</span>))</span><br><span class="line">                    .addStep(StepHelper.checkStep(<span class="string">"允许自启动"</span>, <span class="keyword">true</span>))</span><br><span class="line">                    .addStep(StepHelper.checkStep(<span class="string">"允许关联启动"</span>, <span class="keyword">true</span>))</span><br><span class="line">                    .addStep(StepHelper.checkStep(<span class="string">"允许后台活动"</span>, <span class="keyword">true</span>))</span><br><span class="line">                    .addStep(StepHelper.clickStep(<span class="string">"确定"</span>))</span><br><span class="line">                    .addStep(StepHelper.backStep());</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> assignment;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行任务示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AssignmentFactory</span> </span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">run</span><span class="params">(Activity activity, Queue&lt;AssignmentEntity&gt; queue)</span> </span>&#123;</span><br><span class="line">        L.i(<span class="string">"队列开始执行"</span>);</span><br><span class="line">        <span class="keyword">int</span> assignmentSize = queue.size();</span><br><span class="line"></span><br><span class="line">        CompositeDisposable co = <span class="keyword">new</span> CompositeDisposable();</span><br><span class="line">        Disposable subscribe = Observable.create(</span><br><span class="line">                (ObservableOnSubscribe&lt;AssignmentEntity&gt;) emitter -&gt; &#123;</span><br><span class="line">                    <span class="keyword">int</span> progressSize = <span class="number">0</span>;</span><br><span class="line">                    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; assignmentSize; i++) &#123;</span><br><span class="line">                        AssignmentEntity entity = queue.poll();</span><br><span class="line">                        <span class="keyword">if</span> (entity != <span class="keyword">null</span>) &#123;</span><br><span class="line">                            Queue&lt;StepEntity&gt; stepQueue = entity.getQueue();</span><br><span class="line">                            <span class="keyword">if</span> (stepQueue != <span class="keyword">null</span>) &#123;</span><br><span class="line">                                <span class="keyword">int</span> stepSize = stepQueue.size();</span><br><span class="line">                                <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; stepSize; j++) &#123;</span><br><span class="line">                                    progressSize++;</span><br><span class="line">                                    emitter.onNext(entity);</span><br><span class="line">                                &#125;</span><br><span class="line">                            &#125;</span><br><span class="line">                        &#125;</span><br><span class="line">                    &#125;</span><br><span class="line">                    <span class="comment">//FloatWindowView.getInstance().progressBar.setMax(progressSize);</span></span><br><span class="line">                    emitter.onComplete();</span><br><span class="line">                &#125;)</span><br><span class="line">                .subscribeOn(Schedulers.io())<span class="comment">//执行在io线程</span></span><br><span class="line">                .observeOn(Schedulers.io())<span class="comment">//回调在io线程  //主线程阻塞将无法更新ui AndroidSchedulers.mainThread()</span></span><br><span class="line">                .subscribe(</span><br><span class="line">                        assignment -&gt; &#123;</span><br><span class="line">                            <span class="comment">//FloatWindowView.getInstance().progressAdd();</span></span><br><span class="line">                            <span class="comment">//L.i("当前进度：" + assignment.getName() + "===" + FloatWindowView.getInstance().getProgress());</span></span><br><span class="line">                            poll(activity, assignment.getQueue().poll());</span><br><span class="line">                        &#125;,</span><br><span class="line">                        throwable -&gt; L.e(<span class="string">""</span>, throwable),</span><br><span class="line">                        () -&gt; <span class="comment">//FloatWindowView.getInstance().stopFloatWindow()</span></span><br><span class="line">                );</span><br><span class="line">        co.add(subscribe);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">poll</span><span class="params">(Activity activity, StepEntity poll)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (poll == <span class="keyword">null</span>) <span class="keyword">return</span>;</span><br><span class="line">        <span class="keyword">switch</span> (poll.getType()) &#123;</span><br><span class="line">            <span class="keyword">case</span> StepEntity.TYPE_INTENT:<span class="comment">//跳转事件</span></span><br><span class="line">                activity.startActivity(poll.getIntent());</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> StepEntity.TYPE_CLICK:<span class="comment">//点击事件</span></span><br><span class="line">                MyAccessibilityService.getInstance().clickFirstView(poll.getName());</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> StepEntity.TYPE_CHECKED:<span class="comment">//选中事件</span></span><br><span class="line">                MyAccessibilityService.getInstance().clickCheckBox(poll.getName(), poll.isChecked());</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> StepEntity.TYPE_BACK:<span class="comment">//返回事件</span></span><br><span class="line">                MyAccessibilityService.getInstance().goBack();</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        SystemClock.sleep(POST_DELAY_MILLIS);</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="链接"><a href="#链接" class="headerlink" title="链接"></a>链接</h1><p>完整demo：<a href="https://github.com/yooking-git/Accessibility.git">Github</a></p>
</li>
</ol>

    </div>

    
    
    
        <div class="reward-container">
  <div><font color="#FF0000">------------本文结束<i class="fa fa-paw"></i>感谢您的阅读------------</font><br/><br/>Thank you for your accept！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/imgs/a_icon_pay_wechat.png" alt="yooking 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/imgs/a_icon_pay_ali.png" alt="yooking 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

	  

      <footer class="post-footer">
		<div>
			
				
<div class="my_post_copyright">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script type="text/javascript" src="http://jslibs.wuxubj.cn/sweetalert_mini/jquery-1.7.1.min.js"></script>
  <script src="http://jslibs.wuxubj.cn/sweetalert_mini/sweetalert.min.js"></script>
  <link rel="stylesheet" type="text/css" href="http://jslibs.wuxubj.cn/sweetalert_mini/sweetalert.mini.css">

  <p><span>本文标题:</span>Android辅助功能开发-一键优化</a></p>
  <p><span>文章作者:</span>yooking</a></p>
  <p><span>发布时间:</span>2020年04月24日 - 13:29:35</p>
  <p><span>最后更新:</span>2020年08月24日 - 09:51:42</p>
  <p><span>原始链接:</span><a href="/dev/200424-accessibilityService.html" title="Android辅助功能开发-一键优化">http://blog.yookingh.cn/dev/200424-accessibilityService.html</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://blog.yookingh.cn/dev/200424-accessibilityService.html"  aria-label="复制成功！"></i></span>
  </p>
  <p><span>许可协议:</span>© <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    clipboard.on('success', $(function(){
      $(".fa-clipboard").click(function(){
        swal({   
          title: "",   
          text: '复制成功',   
          html: false,
          timer: 500,   
          showConfirmButton: false
        });
      });
    }));  
</script>

			
		</div>
          
          <div class="post-tags">
              <a href="/tags/study/" rel="tag"><i class="fa fa-tag"></i> 学习笔记</a>
              <a href="/tags/Android/" rel="tag"><i class="fa fa-tag"></i> Android</a>
              <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/dev/191118-bookkeeping.html" rel="prev" title="项目开发-从零开始记账本">
      <i class="fa fa-chevron-left"></i> 项目开发-从零开始记账本
    </a></div>
      <div class="post-nav-item">
    <a href="/dev/200509-audioManager.html" rel="next" title="Android 音量控制">
      Android 音量控制 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#阅读源码"><span class="nav-number">2.</span> <span class="nav-text">阅读源码</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#封装工具"><span class="nav-number">3.</span> <span class="nav-text">封装工具</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#如何使用"><span class="nav-number">4.</span> <span class="nav-text">如何使用</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#链接"><span class="nav-number">5.</span> <span class="nav-text">链接</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="yooking"
      src="/imgs/a_icon_head.jpg">
  <p class="site-author-name" itemprop="name">yooking</p>
  <div class="site-description" itemprop="description"></div>
</div>

<div id="music_163">
	<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" height=86 src="//music.163.com/outchain/player?type=2&id=1417959188&auto=1&height=66"></iframe>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/yooking-git" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;yooking-git" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:android_yoo@126.com" title="E-Mail → mailto:android_yoo@126.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://www.chensheng.group/" title="https:&#x2F;&#x2F;www.chensheng.group&#x2F;" rel="noopener" target="_blank">陈先生的小前端</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://github.com/theme-next" title="https:&#x2F;&#x2F;github.com&#x2F;theme-next" rel="noopener" target="_blank">NexT主题Github</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://hexo-theme-next.netlify.app/" title="https:&#x2F;&#x2F;hexo-theme-next.netlify.app&#x2F;" rel="noopener" target="_blank">NexT主题博客</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2019 – 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yooking</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">118k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">1:47</span>
</div>
<div>
<span id="timeDate">载入天数...</span><span id="times">载入时分秒...</span>
<script>
    var now = new Date();
    function createtime() {
        var grt= new Date("04/07/2019 00:00:00");
        now.setTime(now.getTime()+250);
        days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
        hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
        if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
        mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
        seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
        snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
        document.getElementById("timeDate").innerHTML = "本站已安全运行 "+dnum+" 天 ";
        document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒";
    }
setInterval("createtime()",250);
</script>
</div>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script color='0,0,0' opacity='0.7' zIndex='-1' count='140' src="/lib/canvas-nest/canvas-nest-nomobile.min.js"></script>
  <script size="300" alpha="0.1" zIndex="-1" src="/lib/canvas-ribbon/canvas-ribbon.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>




  




  
<script src="/js/local-search.js"></script>









<script>
document.querySelectorAll('.pdfobject-container').forEach(element => {
  let url = element.dataset.target;
  let pdfOpenParams = {
    navpanes : 0,
    toolbar  : 0,
    statusbar: 0,
    pagemode : 'thumbs',
    view     : 'FitH'
  };
  let pdfOpenFragment = '#' + Object.entries(pdfOpenParams).map(([key, value]) => `${key}=${encodeURIComponent(value)}`).join('&');
  let fullURL = `/lib/pdf/web/viewer.html?file=${encodeURIComponent(url)}${pdfOpenFragment}`;

  if (NexT.utils.supportsPDFs()) {
    element.innerHTML = `<embed class="pdfobject" src="${url + pdfOpenFragment}" type="application/pdf" style="height: ${element.dataset.height};">`;
  } else {
    element.innerHTML = `<iframe src="${fullURL}" style="height: ${element.dataset.height};" frameborder="0"></iframe>`;
  }
});
</script>


<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'default',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'uutufTwYVUeCtemIaV78RUzY-gzGzoHsz',
      appKey     : 'EWtCrVS44F22gkjWkQ15x4lx',
      placeholder: "恭迎评论指教（Your comments and criticisms are always welcome.）",
      avatar     : 'mp',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
